package com.wdf.controller;

import com.wdf.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    private static final String UPLOAD_DIR = "E:/upload/";

    public Result upload(MultipartFile file){
        log.info("上传文件：{}",file);
        if(!file.isEmpty()){
            //生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uniqueFileName = UUID.randomUUID().toString().replace("-","") + extName;
            //拼接
            File targetFile = new File(UPLOAD_DIR + uniqueFileName);

            if(!targetFile.getParentFile().exists()){
                targetFile.getParentFile().mkdirs();
            }
        }
        return Result.success();
    }
}
